//
//  LXScrollADCell.m
//  LvXi_newVersion
//
//  Created by WTFSSD on 2016/11/24.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXScrollADCell.h"

#import "LxRoundScrollView.h"

#import "LXWebViewController.h"

#import "DetailViewController.h"

#import "Model.h"

@interface LXScrollADCell()<LxRoundScrollViewDelegate>

@property(nonatomic,strong)LxRoundScrollView * adScrollView;

@property(nonatomic,strong)NSArray * ads;

@end

@implementation LXScrollADCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self.cellHeight = 150.f;
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.adScrollView = [[LxRoundScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, self.cellHeight)];
        [self.contentView addSubview:self.adScrollView];
    }
    
    return self;
}

-(LXScrollADCell *(^)(NSArray *))setAds{
    LXScrollADCell * (^block)() = ^(NSArray * ads){
        if(!ads||ads.count == 0){
            return self;
        }
        if([ads.firstObject isKindOfClass:[AdvPicModel class]]){
            self.adScrollView.setAds(ads).setEnableTimer(NO).setDelegate(self);
            self.ads = ads;
        }else if([ads.firstObject isKindOfClass:[NSDictionary class]]){
            NSMutableArray *_tempArray = [NSMutableArray array];
            for (NSDictionary * dict in ads) {
                AdvPicModel * _tempModel = [[AdvPicModel alloc] mj_setKeyValues:dict];
                [_tempArray addObject:_tempModel];
            }
        
            self.adScrollView.setAds(_tempArray).setEnableTimer(NO).setDelegate(self);
            self.ads = _tempArray;
        }
        return self;
    };
    return block;
}


-(void)LxRoundScrollView:(LxRoundScrollView *)roundScrollView didClickIndex:(NSInteger)index{
    AdvPicModel * ad = self.ads[index];
    if(index == 0) return;
    if([[ad.url uppercaseString] containsString:@"PRODUCT"]){
        NSRange r1 = [ad.url rangeOfString:@"product"];
        NSRange r2 = [ad.url rangeOfString:@"id"];
        NSString * cat_id = [ad.url substringWithRange:(NSRange){r1.location+r1.length+1,r2.location - (r1.location+r1.length) - 2}];
        [[self getCurrentVC].navigationController pushViewController:[[DetailViewController alloc] init].getProductInfo(cat_id) animated:YES];
        
        return;
    }
    LXWebViewController * webVC = [[LXWebViewController alloc] initWithUrl:ad.url];
    [[self getCurrentVC].navigationController pushViewController:webVC animated:YES];
    
}

@end


NSString * const LXScrollADCellID = @"LXScrollADCell";
